//
//  QuickRechargeViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/5.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "QuickRechargeViewController.h"
#import "LZFoldButton.h"
#import "RechargeApplicationViewController.h"
#import "HighLightsModel.h"

#import "AFNetworking.h"
#import "QuickRechargeModel.h"
#import "RecharMethodObject.h"
#import "BankCodeModel.h"
#import "QuickRecharTableViewCell.h"
@interface QuickRechargeViewController ()<LZFoldButtonDelegate>
{
    // 下拉列表
    LZFoldButton *lz;
    //下拉列表2
    LZFoldButton *firstLZ;
    // 数据model
    HighLightsModel *model;
    NSInteger firstInteger;
    NSInteger secondInteger;
        
    NSMutableArray *_quickRechargeArray;
    NSMutableArray *_methodArray;

}

@end

@implementation QuickRechargeViewController
-(void)obtainMethodData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"get_pay_list" forKey:@"Action"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",arr);
        _methodArray = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            RecharMethodObject *amodel = [[RecharMethodObject alloc] init];
            [amodel setValuesForKeysWithDictionary:dic];
            [_methodArray addObject:amodel];
        }
        [self obtainData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Bank_Config" forKey:@"models"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        for (RecharMethodObject *amodel in _methodArray) {
            amodel.bankArr = [NSMutableArray array];
            for (BankCodeModel *bank in amodel.H_T_P_T_BankCodes) {
                for (NSDictionary *modeldic in [dic objectForKey:@"HX_Bank_Config"]) {
                    
                    QuickRechargeModel *bmodel = [[QuickRechargeModel alloc]init];
                    [bmodel setValuesForKeysWithDictionary:modeldic];
                    if ([bmodel.B_C_Id isEqualToString:bank.b_id]) {
                        [amodel.bankArr addObject:bmodel];
                    }
                }
            }
        }
        [self quickRechargeLZFoldButton];
        [QuickRechargetableView addSubview:lz];
        [self setFirstLZ];
        [QuickRechargetableView addSubview:firstLZ];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    firstInteger = 0;
    secondInteger = 0;
    [self obtainMethodData];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];
    self.title = @"充值";
    
    self.QuickRechargetitles1 = @[@"充值方式",@"选择银行",@"充值金额"];
    
    QuickRechargetableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    QuickRechargetableView.dataSource = self;
    QuickRechargetableView.delegate = self;
    QuickRechargetableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:QuickRechargetableView];

    // 从NSUserDefaults中取出数据
    model = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"]];

    // 快速充值头视图
    UILabel *quickRechargeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 40)];
    quickRechargeLabel.text = [NSString stringWithFormat:@"  当前余额：%@元",model.U_Money];
    quickRechargeLabel.backgroundColor = [UIColor whiteColor];
    QuickRechargetableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    [QuickRechargetableView.tableHeaderView addSubview:quickRechargeLabel];
//    [self quickRechargeLZFoldButton];
}
-(void)setFirstLZ
{
    NSMutableArray *arr = [NSMutableArray array];
    for (RecharMethodObject *amodel in _methodArray) {
        [arr addObject:amodel.H_T_P_T_Nickname];
    } 
    firstLZ = [[LZFoldButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-10-200, 40+20+(40-30)/2, 200, 30) dataArray:arr];
    
    firstLZ.lzDelegate = self;
    firstLZ.lzFontSize = 12;
    firstLZ.lzHeight = 300;
    firstLZ.lzButtonType = 1;
    
    [firstLZ LZSetTitle:[arr firstObject] forState:UIControlStateNormal];
    [firstLZ LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateNormal];
    [firstLZ LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateSelected];
    [firstLZ LZSetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
-(void)quickRechargeLZFoldButton
{
    NSMutableArray *arr = [NSMutableArray array];
    for (QuickRechargeModel *amodel in [_methodArray[firstInteger] bankArr]) {
        [arr addObject:amodel.B_C_Name];
    }
    NSLog(@"%@",arr);
//    NSArray *arr = _quickRechargeArray;
    
    lz = [[LZFoldButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-10-200, 40*2+20+(40-30)/2, 200, 30) dataArray:arr];
    
    lz.lzDelegate = self;
    lz.lzFontSize = 12;
    lz.lzHeight = 300;
    lz.lzButtonType = 1;
    
    [lz LZSetTitle:[arr firstObject] forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateSelected];
    [lz LZSetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

}

-(void)LZFoldButton:(LZFoldButton*)foldButton didSelectObject:(id)obj {
    if (foldButton == firstLZ) {
        for (RecharMethodObject *amodel in _methodArray) {
            if ([amodel.H_T_P_T_Nickname isEqualToString:obj]) {
                firstInteger = [_methodArray indexOfObject:amodel];
            }
        }
        [lz removeFromSuperview];
        [self quickRechargeLZFoldButton];
        [QuickRechargetableView addSubview:lz];
    }
    else{
        
    }
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    } else {
    return 1;
    }
}
//设置表视图中的section（分组）数,默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdent = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //设置数据
    if (indexPath.section == 0) {
        cell.textLabel.text = _QuickRechargetitles1[indexPath.row];
        }else{
        cell.textLabel.text = @"下一步";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:48/255.0f green:173/255.0f blue:224/255.0f alpha:1];
    }
    return cell;
}
//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0 & indexPath.row == 1)
    {
    if (lz.lzSelected) {
        [lz LZCloseTable];
    } else {
        [lz LZOpenTable];
        }
    
    }else if (indexPath.section == 1){
    
    RechargeApplicationViewController *rAVC= [[RechargeApplicationViewController alloc] init];
    [self.navigationController pushViewController:rAVC animated:YES];
        
    }
}
@end
